package com.lznAdmin.system.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.assertj.core.util.Lists;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @program: lzn-manage-parent
 * @description: user实体类
 * @author: lzn
 * @create: 2021-09-02 10:55
 **/
@ApiModel(value = "user对象", description = "用户对象user")
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class SysUser implements UserDetails {

    /**
     * 自增主键
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 用户名
     */
    @NotBlank(message = "用户名不能为空")
    @ApiModelProperty(value = "用户名", required = true)
    private String username;

    /**
     * 密码，加密存储
     */
    @JsonIgnore
    @ApiModelProperty(value = "密码")
    private String password;

    /**
     * 头像图片
     */
    @ApiModelProperty(value = "头像图片")
    private String avatar;

    /**
     * 帐户是否过期(true(1) 未过期，false(0)已过期),
     * <div>设置默认值为true，新增用户默认未过期 *</div>
     * 注意：生成的setter和getter方法没有 `is`
     * <div>setAccountNonExpired</div>
     * getAccountNonExpired
     * <div>所以前端获取时也不要有 `is`</div>
     */
    private boolean isAccountNonExpired = true;

    /**
     * 帐户是否被锁定(true(1) 未过期，false(0)已过期)
     * 设置默认值为true，新增用户默认未过期
     */
    private boolean isAccountNonLocked = true;

    /**
     * 密码是否过期(true(1) 未过期，false(0)已过期)
     * 设置默认值为true，新增用户默认未过期
     */
    private boolean isCredentialsNonExpired = true;

    /**
     * 帐户是否可用(true(1) 可用，false(0)未删除)
     * 设置默认值为true，新增用户默认未过期
     */
    private boolean isEnabled = true;

    /**
     * 昵称
     */
    @ApiModelProperty(value = "昵称")
    private String nickName;

    /**
     * 手机号
     */
    @NotBlank(message = "手机号不能为空！")
    @ApiModelProperty(value = "手机号", required = true)
    private String mobile;

    /**
     * 邮箱
     */
    @ApiModelProperty(value = "邮箱", required = true)
    @NotBlank(message = "邮箱不能为空！")
    @Email(message = "必须是邮箱地址！")
    private String email;


    private Date createDate;

    private Date updateDate;

    /**
     * 拥有权限集合
     */
    @TableField(exist = false) //该属性不是数据库表字段
    private Collection<? extends GrantedAuthority> authorities;
    /**
     * 拥有角色集合
     */
    @TableField(exist = false)
    private List<SysRole> roleList = Lists.newArrayList();
    /**
     * 获取所有角色id
     */
    @TableField(exist = false)
    private List<Long> roleIds = Lists.newArrayList();

    @TableField(exist = false)
    private List<SysPermission> permissions = Lists.newArrayList();

    // 父接口认证方法 start
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.isAccountNonExpired;
    }
    // 父接口认证方法 end

    @Override
    public boolean isAccountNonLocked() {
        return this.isAccountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.isCredentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return this.isEnabled;
    }

    public List<Long> getRoleIds() {
        if (CollectionUtils.isNotEmpty(roleList)) {
            roleIds = Lists.newArrayList();
            for (SysRole role : roleList) {
                roleIds.add(role.getId());
            }
        }
        return roleIds;
    }
}
